<template>
  <div class="my_warp app_content">
    <!-- 导航栏 -->
    <div class="header_body">
      <header-nav :leftArrow="true" titelText="身体记录"></header-nav>
    </div>

    <van-calendar
      @confirm="onConfirm"
      style="height: 80vh"
      v-model:show="info.showCld"
      switch-mode="year-month"
    />
    
    <van-form :label-width="130" @submit="onSubmit">
      <van-field name="time" label="日期">
  				<template #input>
						<div @click="info.showCld = true">{{ form.time }}</div>
					</template>
				</van-field>
        <van-field
          v-model="form.weight"
          name="weight"
          label="体重(公斤)"
          placeholder="体重"
          :rules="[{ required: true, message: '请填写体重' }]"
        />
        <van-field
          v-model="form.waist"
          name="waist"
          label="腰围(cm)"
          placeholder="腰围"
          :rules="[{ required: true, message: '请填写腰围' }]"
        />
        <van-field
          v-model="form.drink"
          name="drink"
          label="饮水(ml)"
          placeholder="饮水"
          :rules="[{ required: true, message: '请填写饮水' }]"
        />
        <van-field
          v-model="form.foodin"
          name="foodin"
          label="摄入(kcal)"
          placeholder="摄入"
          :rules="[{ required: true, message: '请填写摄入' }]"
        />
        <van-field
          v-model="form.sleep"
          name="sleep"
          label="睡眠"
          placeholder="睡眠"
          :rules="[{ required: true, message: '请填写睡眠' }]"
        />
        <div style="margin: 16px">
          <van-button block type="primary" native-type="submit"
            >提交</van-button
          >
        </div>
      </van-form>
    <!-- 底部 -->
    <div class="footer_body">
      <footer-nav></footer-nav>
    </div>
  </div>
</template>

<script setup name="Info">
import moment from 'moment'
import { ref, reactive } from 'vue';
import { showDialog } from 'vant';
import swiperImage from '@/assets/images/defaultUser.jpg';
const router = useRouter();
import { upload, addDaily } from "@/api/http.js";
let form = reactive({
	foodin: '',
	weight: '',
	drink: '',
	waist: '',
	sleep: '',
  userid: '',
  time: '请选择日期'
})

let info = reactive({
  showCld: false,
});

const onSubmit = async (values) => {
  console.log(form)
  if (form.time == '请选择日期') {
    showDialog({
      title: '提醒',
      message: '请选择日期',
    }).then(() => {
      // on close
      console.log(111)
    });
    return
  }
  if (form.taste == '请选择') {
    showDialog({
      title: '提醒',
      message: '请选择口味',
    }).then(() => {
      // on close
      console.log(111)
    });
    return
  }
  addDaily({
		...form,
	}).then((res) => {
    if (res.code == "200") {
      showDialog({
        title: '提醒',
        message: '新增成功',
      }).then(() => {
        router.push("/record");
      });
    } else {
      showDialog({
        title: '提醒',
        message: res.message,
      }).then(() => {
      });
    }
  });
};

const onConfirm = (data) => {
  form.time = moment(data).format("YYYY-MM-DD");
  info.showCld = false;
};

onMounted(() => {
	if (localStorage.getItem('appUserInfo')) {
    let user = JSON.parse(localStorage.getItem('appUserInfo'))
    form.userid = user.id
    console.log(form.userid)
  }
});

</script>
<style lang="less" scoped>
.flex-center {
  display: flex;
  align-items: center;
}
.app_content {
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
  background-color: #f4f4f4;
}

.weights {
  width: 90%;
  height: 60px;
  padding: 20px;
  display: flex;
  justify-content: space-between;
  font-size: 16px;

  .each {
    display: flex;
    flex: 1;
    flex-direction: column;

    div {
      width: 100%;
      text-align: center;
    }
  }
}
.menus {
  width: 82%;
  padding: 20px;
  display: flex;
  flex-direction: column;
  font-size: 14px;

  .each-menu {
    width: 100%;
    height: 50px;
    background: #fff;
    border-radius: 10px;
    padding: 0 20px;
    margin-bottom: 12px;
  }
}
.fs22 {
  font-size: 22px;
  margin-right: 4px;
}
.User {
  width: 100%;
  height: 140px;
  background:-webkit-linear-gradient(top, #ff8198,#f585cb,);
  margin-bottom: 10px;
}
.userImg {
  display: flex;
  align-items: center;
  margin-left: 20px;
  padding-top: 30px;
  padding-bottom: 10px;
  img {
    border-radius: 100%;
    margin-right: 10px;
  }
  span {
    color: white;
    font-weight: 600;
  }
  .sign {
    font-size: 14px;
    color: #333;
    margin-top: 4px;
  }
}
.user-collection {
  ul {
    display: flex;
    align-items: center;
    li {
      display: flex;
      align-items: center;
      flex-direction: column;
      font-size: 14px;
      flex: 1;
      color: white;
    }
  }
}
</style>
